//
//  PSObjectPoolTests.m
//  PrefCore
//
//  Created by Sergey on 5/8/14.
//  Copyright (c) 2014 Play-on-smart. All rights reserved.
//

#import <XCTest/XCTest.h>
#import "PSObjectPool.h"
#import "PSNamedLinkedElement.h"

@interface PSObjectPoolTests : XCTestCase

@end

@implementation PSObjectPoolTests

- (void)testCreate {
    PSObjectPool *p = [[PSObjectPool alloc] initWithCreateObject:^id<PSListEntry>{
        return [PSNamedLinkedElement entryWithName:@""];
    }];
    XCTAssertNotNil(p);
    XCTAssertEqualObjects(@"Pool: 0/0, total=0", [p description]);
}

-(void)testPreallocate {
    @autoreleasepool {
        PSObjectPool *p = [[PSObjectPool alloc] initWithCreateObject:^id<PSListEntry>{
            return [PSNamedLinkedElement entryWithName:@""];
        }];
        [p setCapacity:100];
        XCTAssertEqualObjects(@"Pool: 100/0, total=100", [p description]);
        XCTAssertEqual(100, [PSNamedLinkedElement totalCount]);
        p = nil;
    }
    XCTAssertEqual(0, [PSNamedLinkedElement totalCount]);
}

-(void)testDrain {
    @autoreleasepool {
        PSObjectPool *p = [[PSObjectPool alloc] initWithCreateObject:^id<PSListEntry>{
            return [PSNamedLinkedElement entryWithName:@""];
        }];
        [p setCapacity:100];
        XCTAssertEqualObjects(@"Pool: 100/0, total=100", [p description]);
        XCTAssertEqual(100, [PSNamedLinkedElement totalCount]);
        [p setCapacity:50];
        XCTAssertEqualObjects(@"Pool: 50/0, total=50", [p description]);
        XCTAssertEqual(100, [PSNamedLinkedElement totalCount]);
        p = nil;
    }
    XCTAssertEqual(0, [PSNamedLinkedElement totalCount]);
}

-(void)testGetFromPreallocated {
    @autoreleasepool {
        PSObjectPool *p = [[PSObjectPool alloc] initWithCreateObject:^id<PSListEntry>{
            return [PSNamedLinkedElement entryWithName:@""];
        }];
        [p setCapacity:100];
        XCTAssertEqualObjects(@"Pool: 100/0, total=100", [p description]);
        for (int i = 0 ; i != 90 ; i++) {
            PSNamedLinkedElement *e = [p get];
            XCTAssertNotNil(e);
        }
        XCTAssertEqualObjects(@"Pool: 10/90, total=100", [p description]);
        p = nil;
    }
    XCTAssertEqual(0, [PSNamedLinkedElement totalCount]);
}

-(void)testGetPastPreallocation {
    @autoreleasepool {
        PSObjectPool *p = [[PSObjectPool alloc] initWithCreateObject:^id<PSListEntry>{
            return [PSNamedLinkedElement entryWithName:@""];
        }];
        [p setCapacity:100];
        XCTAssertEqualObjects(@"Pool: 100/0, total=100", [p description]);
        for (int i = 0 ; i != 110 ; i++) {
            PSNamedLinkedElement *e = [p get];
            XCTAssertNotNil(e);
        }
        XCTAssertEqualObjects(@"Pool: 0/110, total=110", [p description]);
        p = nil;
    }
    XCTAssertEqual(0, [PSNamedLinkedElement totalCount]);
}

@end
